package edu.uclm.esi.tysw.podtube.acciones;

import java.sql.SQLException;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.uclm.esi.tysw.podtube.domain.Usuario;
import edu.uclm.esi.tysw.podtube.exceptions.ErrorDeAccesoException;

public class Identificar extends ActionSupport {
	private String email;
	private String pwd;
	private Exception exception;

	public String execute() {
		try {
			Usuario user = new Usuario(this.email, this.pwd);
			ActionContext.getContext().getSession().put("user", user);
			return SUCCESS;
		} catch (ErrorDeAccesoException e) {
			this.exception = e;
			return ERROR;
		} catch (SQLException e) {
			this.exception = e;
			return ERROR;
		}
	}

	public String getEmail() {
		return email;
	}

	public void setQuery(String query) throws ParseException {
		System.out.println(query);
		JSONParser parser = new JSONParser();
		JSONObject jUser = (JSONObject) parser.parse(query);
		this.email = (String) jUser.get("email");
		this.pwd = (String) jUser.get("pwd");
	}

	public void setException(Exception exception) {
		this.exception = exception;
	}

	public Exception getException() {
		return exception;
	}
}
